/*
 * Copyright (C) 2017 The MoonLake Authors
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */


package com.minecraft.moonlake.legendaryweaponrel.weapons;

import com.minecraft.moonlake.MoonLakeAPI;
import com.minecraft.moonlake.api.item.ItemBuilder;
import com.minecraft.moonlake.api.nbt.NBTCompound;
import com.minecraft.moonlake.executor.Consumer;
import com.minecraft.moonlake.legendaryweaponrel.LegendaryWeaponRelPlugin;
import com.minecraft.moonlake.util.StringUtil;
import org.bukkit.Material;
import org.bukkit.Sound;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public abstract class WeaponAbstract implements Weapon {

    protected final String type;
    protected final String displayName;
    protected final Material material;
    protected final static String LORE_PREFIX_CHAT_ATT = StringUtil.decodeUnicode("\u27a3");
    protected final static String LORE_PREFIX_CHAR_CONTENT = StringUtil.decodeUnicode("\u25ac");

    public WeaponAbstract(String type, String displayName, Material material) {
        this.type = type;
        this.displayName = displayName;
        this.material = material;
    }

    @Override
    public String getType() {
        return type;
    }

    @Override
    public String getDisplayName() {
        return displayName;
    }

    @Override
    public Material getMaterial() {
        return material;
    }

    @Override
    public int getData() {
        return 0;
    }

    @Override
    public abstract String[] getLore();

    @Override
    public abstract Consumer<?> onConsumer(Player player);

    @Override
    public ItemStack build() {
        // 构建武器
        ItemBuilder builder = MoonLakeAPI.newItemBuilder(getMaterial(), getData(), 1, getDisplayName());

        // 标签的添加
        List<String> finalLore = new ArrayList<>();
        finalLore.add("");
        finalLore.add("&4&l&m                                   ");
        finalLore.add("&9&l" + LORE_PREFIX_CHAT_ATT + " &6武器属性");
        finalLore.add("");

        // 添加标签函数
        String[] lore = getLore();
        if(lore != null) {
            List<String> weaponLore = Stream.of(lore).parallel().map(raw -> "  &b&l" + LORE_PREFIX_CHAR_CONTENT + " " + raw).collect(Collectors.toList());
            finalLore.addAll(weaponLore);
        }

        finalLore.add("&4&l&m                                   ");
        finalLore.add("&8LegendaryWeaponRel");

        // 添加到武器
        builder.setLore(StringUtil.toColor(finalLore));

        // 调用保护函数让子类进行二次修改
        buildModify(builder);

        // 构建武器
        ItemStack result = builder.build();

        // 如果武器没有附魔则增加一个附魔的发光效果 (仅视觉效果, 无实际效果)
        if(!result.getItemMeta().hasEnchants()) {
            NBTCompound nbt = MoonLakeAPI.getNBTLibrary().readSafe(result);
            nbt.put("ench", MoonLakeAPI.newNBTList());
            nbt.write(result);
        }
        return result;
    }

    protected final LegendaryWeaponRelPlugin getInstance() {
        // 获取插件对象
        return LegendaryWeaponRelPlugin.getInstance();
    }

    protected void buildModify(ItemBuilder builder) {
        // 武器构建器修改
    }

    protected final boolean checkPermission(Player player) {
        // 检测玩家是否拥有武器权限
        return true;

        /*boolean result = player.hasPermission("moonlake.legendaryweapon." + getType());
        if(!result) player.sendMessage(LegendaryWeaponRelPlugin.getInstance().getMessage("NoWeaponPermission"));
        return result;*/
    }

    protected final void healthPlayer(Player player, double amount) {
        // 恢复玩家指定数量的生命值
        if(player.getHealth() + amount >= player.getMaxHealth())
            player.setHealth(player.getMaxHealth()); // 当前声明加上恢复的数量大于等于最大生命则直接恢复
        else player.setHealth(player.getHealth() + amount); // 否则当前上面加上数量
        // 恢复生命播放个音效 2333
        player.playSound(player.getLocation(), Sound.BURP, 10f, 1f);
    }

    protected final void playerUpdateInventory(Player player) {
        // 更新玩家的物品栏
        MoonLakeAPI.runTaskLaterAsync(getInstance(), player::updateInventory, 1L);
    }
}
